<?php

namespace Frieza\Modules\CommunityMcn\Controllers;

use App\Exceptions\InputErrorException;
use App\Http\Controllers\Controller;
use Doraemon\service\CommunityMcn\Suggestion as sSuggestion;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use App\Pockets\GridDataModel as pGridDataModel;
use Illuminate\Support\MessageBag;
use Doraemon\model\CommunityMcn\Brief;

class SuggestionController extends Controller
{
    public function index(Content $content, Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('per_page', 20);
        $bId = $request->input('b_id', 0);
        $limit = [($page - 1) * $pageSize, $pageSize];
        $where = ['b_id' => $bId];
        $data = sSuggestion::getListByWhere($where, $limit);
        $number = $data['total'] - ($page - 1) * $pageSize;
        foreach ($data['list'] as &$val) {
            $val['number'] = $number--;
        }
        $gridModel = new pGridDataModel($data['list'], $data['total'], [
            'perPage' => $pageSize,
        ]);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->expandFilter();
            $grid->disableFilter();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->expand();
            });
            $grid->column('id', '序号')->display(function ($released) {
                return $this->number;
            })->width(80);
            $grid->column('content', '内容')->display(function ($released) {
                return $released;
            })->width(500)->limit(500);
            $grid->column('addtime', '时间')->display(function ($released) {
                return date('Y-m-d H:i:s', $released);
            })->width(100);

            $grid->disableActions();
        });
        return $content
            ->header('审核列表')
            ->breadcrumb(['text' => '审核列表', 'url' => '/admin/circle/black'])
            ->body($listGrid);
    }

    public function create(Content $content, Request $request)
    {
        $bId = $request->get('b_id', 0);
        if (empty($bId)) {
            $error = new MessageBag([
                'title' => '请重新进入审核列表'
            ]);
            return redirect('/admin/communityMcn/briefList/list')->with(compact('error'));
        }
        $form = new Form();
        $form->textarea('content', '审核意见')->required();
        $form->hidden('b_id', '审核意见')->default($bId);
        $form->action('/admin/communityMcn/suggestion/add');

        return $content
            ->header('审核意见')
            ->breadcrumb(['text' => 'MCN', 'url' => 'circle'], ['text' => '审核意见'])
            ->row($form->render());
    }

    public function add(Request $request)
    {
        $data['content'] = $request->input('content', '');
        $data['b_id'] = $request->input('b_id', 0);
        $data['admin_id'] = Admin::user()->id;
        if (empty($data['content'])) {
            throw new InputErrorException('请输入审核意见', InputErrorException::DEFAULT_CODE, 'content');
        }
        if (empty($data['b_id'])) {
            throw new InputErrorException('请重新从Brief列表进入', InputErrorException::DEFAULT_CODE, 'content');
        }
        Brief::updateById($data['b_id'],['status' => Brief::STATUS_CHANGE]);
        sSuggestion::add($data);
        $message = '保存成功';
        $success = new MessageBag([
            'title' => $message
        ]);
        //todo 修改地址跳详情
        return redirect('/admin/communityMcn/suggestion/index?b_id='.$data['b_id'])->with(compact('success'));
    }
}